package view
{
	import AIRProject.filemanager.FileScaner;
	
	import com.greensock.TweenLite;
	
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.utils.setTimeout;
	
	import view.ui.CutOutWorker;
	import view.ui.ProgressView;
	import view.ui.TitleTextField;
	
	public class WorkView extends Sprite
	{
		[Embed(source="/asset/cancel.png")]
		protected var _cancel_jpg:Class;
		protected var cancel_jpg:Bitmap;
		private var cancle_bt:Sprite;
		private var title:TitleTextField;
		private var browswerProgress:ProgressView;
		private var fileScaner:FileScaner;
		private var pngFiles:Array
		private var handleDir:File;
		private var exportDir:File;
		private var total:Number;
		private var curNum:uint=0;
		private var workers:Vector.<CutOutWorker>;
		private var findPng:Boolean=false;
		private var totalProgress:ProgressView;
		public function WorkView(h:File,e:File)
		{
			super();
			handleDir=h;
			exportDir=e;
			init();
		}
		
		private function init():void
		{
			title=new TitleTextField();
			title.text="程序运行中";
			this.addChild(title);
			title.x=40;
			browswerProgress=new ProgressView();
			browswerProgress.x=10;
			browswerProgress.y=70;
			this.addChild(browswerProgress);
			browswerProgress.text="正在扫描文件夹";
			browswerProgress.progress(5);
			scanFolder();
			cancel_jpg=new _cancel_jpg() as Bitmap;
			cancel_jpg.smoothing=true;
			cancle_bt=new Sprite();
			cancle_bt.addChild(cancel_jpg);
			this.addChild(cancle_bt);
			cancle_bt.x=550;
			cancle_bt.y=250;
			cancle_bt.visible=false;
		}
		
		private function handleCancelBtClick(e:MouseEvent):void
		{	
			out();
		}
		
		private function scanFolder():void
		{
			fileScaner=new FileScaner();
			fileScaner._filters=["png","jpg"];
			fileScaner.scanFolderAsync(handleDir);
			fileScaner.addEventListener("folderScaned",handleFolderScanOver);
			
		}
		
		private function handleFolderScanOver(e:MyEvent):void
		{
			pngFiles=e.obj;
			total=pngFiles.length;
			browswerProgress.text="文件夹扫描完毕";
			browswerProgress.progress(10);
			readyWork();
		}
		
		private function readyWork():void
		{
			if(pngFiles.length>0)
			{
				findPng=true;
				cancle_bt.visible=true;
				cancle_bt.addEventListener(MouseEvent.CLICK,handleCancelBtClick);
				browswerProgress.text="准备工作完成";
				browswerProgress.progress(100);
				TweenLite.to(browswerProgress,0.5,{delay:1,alpha:0,y:browswerProgress.y-100,onComplete:removeObject,onCompleteParams:[browswerProgress as Sprite]});
			}
			else
			{
				browswerProgress.text="未找到PNG文件";
				browswerProgress.progress(100);
				browswerProgress.red();
				TweenLite.to(browswerProgress,0.5,{delay:2,alpha:0,y:200,onComplete:removeObject,onCompleteParams:[browswerProgress as Sprite]});
			}
			
			
			
		}
		
		private function out():void
		{
			if(browswerProgress)
			{
				TweenLite.killTweensOf(browswerProgress);
			}
			if(cancle_bt.hasEventListener(MouseEvent.CLICK))
			{
				cancle_bt.removeEventListener(MouseEvent.CLICK,handleCancelBtClick);
			}
			removeWorkers();
			this.dispatchEvent(new MyEvent("workOut"));
		}
		
		private function removeWorkers():void
		{
			if(workers)
			{
				var length:uint=workers.length;
				var worker:CutOutWorker;
				for(var i:uint=0;i<length;i++)
				{
					worker=workers[i];
					if(worker)
					{
						if(worker.hasEventListener("fileOk"))
						{
							worker.removeEventListener("fileOk",handleWorkerComplete);
							worker.removeEventListener("fileError",handleWorkerError);
						}
						worker.stopWork();
					}
					
				}
				workers=null;
			}
		}
		
		private function removeObject(s:Sprite):void
		{
			this.removeChild(s);
			if(s==browswerProgress)
			{
				browswerProgress=null;
				if(!findPng)
				{
					this.dispatchEvent(new MyEvent("workOut"));
				}
				else
				{
					makeTotalProgress();
					makeNextWorker(curNum);
					curNum++;
					makeNextWorker(curNum);	
				}
				
			}
			else
			{
				if(s is CutOutWorker)
				{
					var worker:CutOutWorker= CutOutWorker(s) ;
					worker.stopWork();
					if(worker.hasEventListener("fileOk"))
					{
						worker.removeEventListener("fileOk",handleWorkerComplete);
						worker.removeEventListener("fileError",handleWorkerError);
					}
				}
					
				curNum++;
				if(curNum>total-1)
				{
					
				}
				else
				{
					makeNextWorker(curNum);
				}
			}
			
			
		}
		
		private function makeTotalProgress():void
		{
			totalProgress=new ProgressView();
			this.addChild(totalProgress);
			totalProgress.x=400;
			totalProgress.y=10;
			
			
		}
		
		private function updateProgress(value:uint):void
		{
			totalProgress.text="共有"+total+"张图片,现在已经处理了"+value+"张";
			totalProgress.progress(value/total*100);
		}
		
		private function makeNextWorker(value:Number):void
		{
			if(!workers)
			{
				workers=new Vector.<CutOutWorker>(total);
			}
			
			var worker:CutOutWorker=new CutOutWorker(pngFiles[value],exportDir);
			worker.index=value;
			worker.addEventListener("fileOk",handleWorkerComplete);
			worker.addEventListener("fileError",handleWorkerError);
			this.addChild(worker);
			worker.x=10;
			workers[value]=worker;
			worker.alpha=0;
			worker.y=280;
			if(value==0)
			{
				totalProgress.text="共有"+total+"张图片,现在正在处理第"+value+"张";
				totalProgress.progress(1);
				TweenLite.to(worker,0.5,{alpha:1,y:70,onComplete:worker.startWork});
			}
			else
			{
				worker.wait();
				TweenLite.to(worker,0.5,{alpha:0.5,y:180});
			}
			
			
		}
		
		private function handleWorkerComplete(e:MyEvent):void
		{
			var num:uint=e.obj;
			TweenLite.to(workers[num],0.5,{delay:1,alpha:0,y:workers[num].y-100,onComplete:removeObject,onCompleteParams:[workers[num] as Sprite]});
			updateProgress(num+1);
			if(num!=total-1)
			{
				num++;
				TweenLite.to(workers[num],0.5,{delay:1,y:70,alpha:1,onComplete:workers[num].startWork});
			}
			else
			{
				setTimeout(out,2000);
			}
		}
		
		private function handleWorkerError(e:MyEvent):void
		{
			TweenLite.to(workers[e.obj],0.5,{delay:1,alpha:0,y:workers[e.obj].y-100,onComplete:removeObject,onCompleteParams:[workers[e.obj] as Sprite]});
			var num:uint=e.obj;
			totalProgress.red();
			updateProgress(num+1);
			if(num!=total-1)
			{
				num++;
				
				TweenLite.to(workers[num],0.5,{delay:1,y:70,onComplete:workers[num].startWork});
			}
			else
			{
				setTimeout(out,2000);
			}
		}
		
	}
}